package com.newhope.carqueue

import android.animation.ObjectAnimator
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowInsetsController
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import io.dcloud.uniapp.UniAppActivity

class SplashActivity : AppCompatActivity() {
    
    private lateinit var splashLogo: ImageView
    private lateinit var loadingContainer: View
    private lateinit var loadingProgress: ProgressBar
    private lateinit var loadingText: TextView
    private lateinit var versionText: TextView
    
    private val handler = Handler(Looper.getMainLooper())
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        
        // 设置状态栏和导航栏样式
        setupStatusBar()
        
        initViews()
        startAnimations()
    }
    
    private fun setupStatusBar() {
        // 主题已经设置了基本的状态栏样式，这里只做兼容性处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 确保状态栏图标为深色（浅色状态栏）
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        
        // 确保导航栏图标为深色（Android 8.1+）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            window.decorView.systemUiVisibility = window.decorView.systemUiVisibility or 
                View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
        }
    }
    
    private fun initViews() {
        splashLogo = findViewById(R.id.splash_logo)
        loadingContainer = findViewById(R.id.loading_container)
        loadingProgress = findViewById(R.id.loading_progress)
        loadingText = findViewById(R.id.loading_text)
        versionText = findViewById(R.id.version_text)
        
        // 动态设置版本号
        setVersionText()
    }
    
    private fun setVersionText() {
        try {
            val packageInfo = packageManager.getPackageInfo(packageName, 0)
            val versionName = packageInfo.versionName
            versionText.text = "v $versionName"
        } catch (e: Exception) {
            // 如果获取失败，使用默认版本号
            versionText.text = "v 1.0.0"
        }
    }
    
    private fun startAnimations() {
        // Logo动画 - 缩放淡入
        handler.postDelayed({
            val logoAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_fade_in)
            splashLogo.startAnimation(logoAnimation)
            splashLogo.alpha = 1f
        }, 300)
        
        // 加载容器动画
        handler.postDelayed({
            val loadingAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in)
            loadingContainer.startAnimation(loadingAnimation)
            loadingContainer.alpha = 1f
            startProgressAnimation()
        }, 800)
        
        // 版本信息动画
        handler.postDelayed({
            val versionAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in)
            versionText.startAnimation(versionAnimation)
            versionText.alpha = 1f
        }, 1200)
        
        // 启动主应用
        handler.postDelayed({
            startMainActivity()
        }, 3000)
    }
    
    private fun startProgressAnimation() {
        val progressAnimator = ObjectAnimator.ofInt(loadingProgress, "progress", 0, 100)
        progressAnimator.duration = 1800
        progressAnimator.start()
        
        // 更新加载文本
        val loadingTexts = arrayOf(
            "正在启动...",
            "加载资源中...",
            "初始化系统...",
            "准备就绪"
        )
        
        loadingTexts.forEachIndexed { index, text ->
            handler.postDelayed({
                loadingText.text = text
            }, index * 450L)
        }
    }
    
    private fun startMainActivity() {
        val intent = Intent(this, UniAppActivity::class.java)
        startActivity(intent)
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        finish()
    }
    
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        // 处理屏幕旋转，重新设置状态栏样式
        setupStatusBar()
    }
    
    override fun onBackPressed() {
        // 在启动画面期间禁用返回键
    }
}